package com.walker.jdkapi.masterworker;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;

import org.springframework.util.ObjectUtils;

public class Worker implements Runnable {
	
	private ConcurrentLinkedQueue<Product> concurrentLinkedDeque;
	
	private ConcurrentHashMap<String, Object> resultMap;


	@Override
	public void run() {
		while(true) {
			Product product = this.concurrentLinkedDeque.poll();
			//队列中无要出来的数据，线程结束
			if(ObjectUtils.isEmpty(product)) {
				break;
			}else {
				Object returnData = this.handle(product);
				this.resultMap.put(Integer.toString(product.getId()), returnData);
			}
		}

	}
	
	private Object handle(Product product) {
		Object price = null;
		try {
			TimeUnit.SECONDS.sleep(1);
			price = product.getPrivce();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return price;
	}

	public void setConcurrentLinkedDeque(ConcurrentLinkedQueue<Product> concurrentLinkedDeque) {
		this.concurrentLinkedDeque = concurrentLinkedDeque;
	}

	public void setResultMap(ConcurrentHashMap<String, Object> resultMap) {
		this.resultMap = resultMap;
	}
	
	
}
